#    Name: countyCoordinateMaker.py
#    Description:  Modified from zipCodeMaker.py to parse files that are formatted differently.
#                    This parses a txt file that lists a COUNTY followed by a line that gives all the coordinates for that county.
#                    It also parsed the state lines by putting a state in the place of a name of a county.
#                    See washingtonCounties.txt -> washingtonCounties.js to see an example of the results of the script.
#                    ***NOTE: Do not use this file to overwrite the files currently in the directory! A few manual edits had to be made
#                    on these files in order to account for differences in the way county names are written and also to account for 
#                    states with multiple polygons. You may try to run it, but don't expect the files to still work with the application :)***
#    Revision History:
#    10/13/2011 Chris Lewis: Commented the code and put final polish on the file
#    10/10/2011 Chris Lewis: Modified script to parse a file of a different format.
#    9/2011 tlc: Initial version.

import csv
import datetime

#read the file. If it has a COUNTY on the line, take the name of the county and make that a function
#make a file like stateOverlay.js
#var stateBorders = {
#'WA' : [new google.maps.LatLng(48.400032, -124.628906), new google.maps.LatLng(45.828800, -123.925781), new google.maps.LatLng(46.134171, -117.158203), new google.maps.LatLng(48.980217, -117.158203), new google.maps.LatLng(48.922501, -123.046875), new google.maps.LatLng(48.224674, -123.134766), new google.maps.LatLng(48.400032, -124.628906)],
#}
# Read in Longitude and Latitude Information
# This name was changed manually for each file made.
reader = csv.reader(open('hawaiiCounties.txt'), delimiter = '\n')

# Open the .js file that will be generated.  Note that if the
# file already exists, it will be over-written.
# This name was changed manually for each file made.
output = open('hawaiiCounties.js', 'w')

# Get the current date and time to timestamp
# the automatically generated file.
now = datetime.datetime.now()

# Write the comment that will head the file
output.write("// This file was automatically generated on ")
output.write(now.strftime("%Y-%m-%d %H:%M. \n"))
output.write("// DO NOT EDIT. Use countyCoordinateMaker.py to generate. \n\n\n")

# Write the function header
# This name was changed manually for each file made.
output.write("function constructHawaiiCounties() \n")
output.write("{ \n\n")

# Begin the array of geographical points
output.write("    var countyCoordinates = { \n")

for row in reader:
    try:
        if(row[0].find('COUNTY') != -1):
            splitRow = row[0].partition(' ')
            currentCounty = splitRow[2]
            output.write("'"+currentCounty+"' : [")
        else:
            newRow = row[0].split()
            for x in newRow:
                try:
                    splitX = x.partition(',')
                    lat = splitX[2]
                    lon = splitX[0]

                    # Construct the call to make a new geographical point using the
                    # longitude and latitude read from the ZCTA information.   

                    output.write("new google.maps.LatLng(")
                    output.write(lat)
                    output.write(",")
                    output.write(lon)
                    output.write("),")
                except:
                    pass
            output.write("],\n");
                
                
    except:
        pass


    
# Complete the array
output.write("\n    };\n\n")

# Complete the function with a return value and a final curly bracket.
output.write("    return countyCoordinates\n")
output.write("}")
